<?php
/*
  Plugin Name: Edit image color
  Description: Edit image color with instagram effect.
  Plugin URI: http://eyered.net
  Version: 1.0.0
  Author: Vchip
  Author URI: http://eyered.net
 */

If (!Class_Exists('wp_plugin_edit_image_color')) {

    class wp_plugin_edit_image_color {

        function __construct() {
            Add_Action('admin_menu', Array($this, 'Add_Options_Page'));
            Add_Action('admin_enqueue_scripts', Array($this, 'Enqueue_Backend_Scripts'));
        }

        function Enqueue_Backend_Scripts() {
            wp_enqueue_script(
                    'jquery', plugins_url('/js/jquery.js', __FILE__)
            );
            wp_enqueue_script(
                    'jquery_ui', plugins_url('/js/jquery.ui.js', __FILE__)
            );
            wp_enqueue_script(
                    'jcarousel', plugins_url('/js/jquery.jcarousel.min.js', __FILE__)
            );
            wp_enqueue_script(
                    'caman', plugins_url('/js/caman.full.min.js', __FILE__)
            );
            wp_enqueue_script(
                    'pixastic', plugins_url('/js/pixastic.js', __FILE__)
            );
            wp_enqueue_script(
                    'plugin', plugins_url('/js/plugin.js', __FILE__)
            );
            wp_enqueue_style(
                    'plugin_css', plugins_url('/css/plugin.css', __FILE__)
            );
            wp_enqueue_style(
                    'jquery_css', plugins_url('/css/jquery.css', __FILE__)
            );
        }

        function Add_Options_Page() {
            if (isset($_REQUEST['attachment_id'])) {
                if (isset($_REQUEST['url'])) {
                    $url = $_REQUEST['url'];
                    $img = basename(wp_get_attachment_url($_REQUEST['attachment_id']));
                    $img = explode(".", $img);
                    $img_ext = $img[count($img) - 1];
                    $img[count($img) - 1] = "";
                    $new_img = implode("", $img) . '_' . $_REQUEST['color_select'] . "." . $img_ext;
                    $new_image_name = $new_img;
                    file_put_contents($new_img, file_get_contents($url));
                    $ImgInfo = getimagesize($new_img);
                    $wp_upload_dir = wp_upload_dir();
                    imagepng(imagecreatefrompng($new_img), $wp_upload_dir['path'] . '/' . basename($new_image_name));
                    //$new_img = imagecreatefrompng($wp_upload_dir['path'] . '/' . basename( $new_image_name ));
                    $attachment = array(
                        'guid' => $wp_upload_dir['url'] . '/' . basename($new_image_name),
                        'post_mime_type' => $ImgInfo['mime'],
                        'post_title' => preg_replace('/\.[^.]+$/', '', basename($new_image_name)),
                        'post_content' => '',
                        'post_status' => 'inherit'
                    );
                    $attach_id = wp_insert_attachment($attachment, $wp_upload_dir['path'] . '/' . basename($new_image_name));
                    // you must first include the image.php file
                    // for the function wp_generate_attachment_metadata() to work
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $attach_data = wp_generate_attachment_metadata($attach_id, $wp_upload_dir['path'] . '/' . basename($new_image_name));
                    wp_update_attachment_metadata($attach_id, $attach_data);
                    echo '1';
                    die;
                } else {
                    $handle = Add_Menu_Page(
                            'Change color', 'Edit image color', 'manage_options', __CLASS__, Array($this, 'Print_Color_Page')
                    );
                }
            } else {
                $handle = Add_Menu_Page(
                        'List images', 'Edit image color', 'manage_options', __CLASS__, Array($this, 'Print_Options_Page')
                );
            }
        }

        function Print_Color_Page() {
            $image = wp_get_attachment_url($_REQUEST['attachment_id']);
            $image_thumbnail = wp_get_attachment_image_src($_REQUEST['attachment_id'], 'thumbnail');
            $post = wp_get_single_post($_REQUEST['attachment_id']);
            $this->post = $post;
            $this->image = $image;
            $this->image_thumbnail = $image_thumbnail[0];
            Include DirName(__FILE__) . '/options-page/color-page.php';
        }

        function Print_Options_Page() {
            Include DirName(__FILE__) . '/options-page/options-page.php';
        }

    }

    /* End of the Class */
    New wp_plugin_edit_image_color;
    add_filter('media_row_actions', 'add_edit_color_to_media_rows', 10, 2);
    add_filter('attachment_submitbox_misc_actions', 'add_edit_color_to_edit_media', 10, 2);
}

function add_edit_color_to_media_rows($actions, $post) {
    $post_type_object = get_post_type_object($post->post_type);
    $can_edit_post = current_user_can($post_type_object->cap->edit_post, $post->ID);
    if ($can_edit_post) {
        $actions['edit_image_color'] = '<a href="' . admin_url('admin.php?page=wp_plugin_edit_image_color&attachment_id=' . $post->ID) . '" title="' . esc_attr(__('Edit this item')) . '">' . __('Edit Image Color') . '</a>';
    }
    return $actions;
}

function add_edit_color_to_edit_media() {
    $post = get_post();
    if (isset($_GET['post'])) {
        ?>
        <div class="misc-pub-section">
            <label for="attachment_url">Edit Image Color:</label>
            <a href="<?PHP echo admin_url('admin.php?page=wp_plugin_edit_image_color&attachment_id=' . $post->ID)?>">Edit</a>
        </div>
        <?php
    }
}
